import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

import { UserService } from '@services/common/user.service';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { AuthService } from '@core/services/common/auth.service';
@Directive({
  selector: '[appAuth]'
})
export class AuthDirective {
  codeArray!: string[];

  @Input()
  set appAuth(authCode: string | undefined) {
    if (!authCode) {
      this.show(true);
      return;
    }
    console.log('authCode',authCode);
    this.codeArray.includes(authCode) ? this.show(true) : this.show(false);
  }

  constructor(
    private userService: UserService,
    private templateRef: TemplateRef<NzSafeAny>,
    private viewContainerRef: ViewContainerRef,
    private authService: AuthService
    ) {
    const userInfo = this.userService.getUserInfo();
    this.codeArray = this.authService.getAuthorizedAccessResource();
  }

  private show(hasAuth: boolean): void {
    hasAuth ? this.viewContainerRef.createEmbeddedView(this.templateRef) : this.viewContainerRef.clear();
  }
}
